const express = require("express");
const router = express.Router();
const db = require("../db/index.js");
const jwt = require("jsonwebtoken");
const secretKey = require("../config.js");

router.post("/login",(req,res)=>{
    const userinfo = req.body;
    const sqlStr = "select * from db_users where username = ? ";
    db.query(sqlStr,userinfo.username,(err,result)=>{
        if(err){
            return res.cc(err)
        }else{
            if(result.length==0){
                return res.cc("用户名不存在")
            }else if(result[0].password != userinfo.password){
                return res.cc("密码不正确")
            }else {
                const user = {...result[0],password:""};
                const tokenstr = jwt.sign(user,secretKey.jwtSecretKey,{expiresIn:"1h"});
                return res.send({
                    status:0,
                    message:"登录成功",
                    token:"Bearer "+tokenstr
                })  
            }
        }
   })
})






module.exports = router;